#include "teachflyitem.h"
#include "toolkit.h"
#include <QPainter>
#include "resources.h"

TeachFlyItem::TeachFlyItem():
    m_bOpening(false),
    m_iIndicateTime(0),
    m_bBeginShowIndicate(false)
{
    m_IndicateImage.load(":/image/fly/zhishijiantou.png");
}

void TeachFlyItem::FlyFloat()
{
    if(m_bUp)
    {
        if(y() > m_detectY)
        {
            setY(y() - 4);
        }
        else
        {
            if(m_bOpening)
            {
                if(ToolKit::m_bVoice && ToolKit::m_bgameVoice)
                    ToolKit::playSound(7);

                m_bFloat = false;
                SetGateTp(OPEN_HTP);
            }
            else
            {
                m_bUp = false;
                m_detectY = qrand() % static_cast<int>(FLOAT_RANGE / 2)
                        + (FLOAT_RANGE / 2);
            }
        }
    }
    else
    {
        if(y() < m_detectY)
        {
            setY(y() + 4);
        }
        else
        {
            m_bUp = true;
            m_detectY = qrand() % static_cast<int>(FLOAT_RANGE / 2);
        }
    }
}

void TeachFlyItem::Timing()
{
    if(m_bBeginShowIndicate)
    {
        if(m_bShowIndicate)
        {
            if(m_iIndicateTime > 2)
            {
                m_bShowIndicate = false;
            }
            else
            {
                ++m_iIndicateTime;
            }
        }
        else
        {
            if(m_iIndicateTime < 0)
            {
                m_bShowIndicate = true;
            }
            else
            {
                --m_iIndicateTime;
            }
        }
    }
}

void TeachFlyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if(m_bShine)
    {
        m_bShine = false;
        painter->drawPixmap(QRectF(30.0,21.0,114.0,80.0),ToolKit::m_rc->m_FlyShineImage,m_flyRect);
    }
    else
    {
        if(m_boatTp == OVER_BTP)
            painter->drawPixmap(QRectF(0.0,0.0,156.0,127.0),m_flyOverImage,m_flyOverRect);
        else
            painter->drawPixmap(QRectF(30.0,21.0,114.0,80.0),m_flyImage,m_flyRect);
    }

    if(m_boatTp == NORMAL_BTP || m_boatTp == BOMBED_BTP)
    {
        switch(m_hatchTp)
        {
        case COLSE_HTP:
        case COLSING_HTP:
        case OPEN_HTP:
        {
            painter->drawPixmap(QRectF(65.0,86.0,45.0,7.0),ToolKit::m_rc->m_FlyGateImage,m_gateRect);
        }
            break;
        case LIGHT_HTP:
        {
            painter->drawPixmap(QRectF(66.0,88.0,41.0,40.0),ToolKit::m_rc->m_FlyLightImage,m_lightRect);
        }
            break;
        case WARN_HTP:
        {
            if(m_bShowWarnImage)
            {
                m_bShowWarnImage = false;
                painter->drawPixmap(QRectF(66.0,87.0,43.0,5.0),ToolKit::m_rc->m_FlyWarningImage,
                                    QRectF(m_WarningImageX,0.0,43.0,5.0));
            }
        }
            break;
        default:
            break;
        }
    }

    if(m_bBeginShowIndicate)
    {
        if(m_bShowIndicate)
            painter->drawPixmap(QRectF(70.0,128.0,36.0,47.0),m_IndicateImage,
                                QRectF(0.0,0.0,36.0,47.0));
    }
}

QRectF TeachFlyItem::boundingRect() const
{
    return QRectF(0.0,0.0,156.0,134.0);
}
